﻿using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate;
using System.Reflection;
using NHibernate.Mapping.Attributes;
using Bamboo.Core.Data.Hibernate;
using TinyERP.Framework.Plugin;
namespace TinyERP.Framework
{
    public class LocalSessionFactoryObjectImp : LocalSessionFactoryObject
    {
        protected override void PostProcessConfiguration(NHibernate.Cfg.Configuration config)
        {
            base.PostProcessConfiguration(config);
            HbmSerializer.Default.Validate = true;
            IList<Module> modules = ApplicationConfiguration.GetInstance().Modules;
            foreach (Module module in modules)
            {
                Assembly assemblyInAppDomain = FindAssembly(module.Assembly);
                if (assemblyInAppDomain != null)
                {
                    config.AddInputStream(HbmSerializer.Default.Serialize(assemblyInAppDomain));
                }
            }
        }
        private Assembly FindAssembly(string assemblyName)
        {
            Assembly[] assArray = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assArray)
            {
                if (assembly.GetName().Name == assemblyName)
                {
                    return assembly;
                }
            }
            return null;
        }
    }
}
